//
//  FriendViewController.m
//  QSBK
//
//  Created by Eric Wang on 5/6/15.
//  Copyright (c) 2015 Eric Wang. All rights reserved.
//
#import "ChatViewController.h"
#import "User.h"
#import "AppDelegate.h"
#import "AFNetworking.h"
#import "FriendViewController.h"
#import "FriendListCell.h"
@interface FriendViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *friendListArray;
//@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *myDataArray;
@end

@implementation FriendViewController
-(void)setUser:(User *)user
{
    _user=user;
    [self getFriendList:_user];
}
-(NSMutableArray *)friendListArray
{
    if(_friendListArray==nil)
    {
        _friendListArray = [[NSMutableArray alloc]init];
    }
    return _friendListArray;
}
//-(void)configNothingFriendList
//{
//    [self.tableView reloadData];
//   
//}
-(void)addAllSubviews
{

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

-(void)getFriendList:(User *)user
{
    if(_user!=nil)
    {

        NSString *urlString = @"http://localhost:8888/thinkphp_3.2.3/Home/QSBK/getfriends?";
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *para = @{@"user_id":_user.user_id
                               };
        [manager GET:urlString parameters:para success:^(AFHTTPRequestOperation *operation,id responseObject){
            NSArray *friendList = [((NSDictionary *)responseObject) objectForKey:@"friends"];
            //NSLog(@"%@",friendList);
            for(NSDictionary *dic in friendList)
            {
                User *user = [[User alloc]init];
                user.user_id = [dic objectForKey:@"user_id"];
                user.articleTotal = [dic objectForKey:@"total"];
                user.stater = [dic objectForKey:@"stater"];
                user.role = [dic objectForKey:@"role"];
                user.longitude = [dic objectForKey:@"longitude"];
                user.latitude = [dic objectForKey:@"latitude"];
                user.login = [dic objectForKey:@"login"];
                user.last_visited_at = [dic objectForKey:@"last_visited"];
                user.last_device = [dic objectForKey:@"last_device"];
                user.icon = [dic objectForKey:@"icon"];
                user.email = [dic objectForKey:@"email"];

                
                [self.friendListArray addObject:user];
                
            }
           
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
            
        }failure:^(AFHTTPRequestOperation *operation,NSError *error)
        {
            NSLog(@"%@",error);
        }];
    }
//    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.myDataArray = [[NSMutableArray alloc]init];
    [self addAllSubviews];
    // Do any additional setup after loading the view from its nib.
}
//#pragma self.tableview's datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.friendListArray.count;
}
//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FriendListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(cell==nil)
    {
        cell = [[FriendListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.user = self.friendListArray[indexPath.row];
    
    return cell;
}
//
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
////    FriendListCell *cell = (FriendListCell *)[tableView cellForRowAtIndexPath:indexPath];
////    if(self.delegate!=nil&&[self.delegate respondsToSelector:@selector(turnToChatViewController:)])
////    {
////        [self.delegate turnToChatViewController:cell.user];
////    }
//    
//
    
    ChatViewController *_conversationVC = [[ChatViewController alloc]init];
    _conversationVC.conversationType = ConversationType_PRIVATE;
    _conversationVC.targetId = [NSString stringWithFormat:@"%d",1024];
    _conversationVC.targetName = @"丫丫";
    _conversationVC.title = @"YY";
    
    
    [self presentViewController:_conversationVC animated:YES completion:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(void)viewWillAppear:(BOOL)animated
{
    
}



@end
